package com.example.pachong.quanliang;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @version 1.0
 * @Author gxh
 * @Date 2022/8/25 15:52
 */
@Slf4j
public class CrawlMsg {


    private static String url = "https://www.aquanliang.com/blog/page/";

    private static List<Entity> entities = new ArrayList<>();

    public static void main(String[] args) throws IOException, InterruptedException {
        log.info("开始数据爬取");
        int page = getPage() + 1;
        for (int i = 1; i < page; i++) {
            getMsg(url+i);
        }
        log.info("数据爬取完成，开始输出");
        EasyExcel.write("圈量科技.xlsx", Entity.class).sheet("模板").doWrite(entities);
        log.info("数据输出成功共"+entities.size()+"条->"+System.getProperty("user.dir"));
    }

    /**
     * 获取单页的数据
     * @param u
     * @throws IOException
     * @throws InterruptedException
     */
    public static void getMsg(String u) throws IOException, InterruptedException {
        //TODO 设置代理IP
        System.out.println("网址为:"+u+"---->爬取中");
        Document document = Jsoup.connect(u).get();
        //每个文章的总
        Elements titles = document.getElementsByClass("_3gcd_TVhABEQqCcXHsrIpT");
        for (Element title : titles) {
            //图片地址
            String url = title.select("img").get(0).attr("src");
            //文章标题
            String name = title.getElementsByClass("_3_JaaUmGUCjKZIdiLhqtfr").html();
            //发布时间
            String date = title.getElementsByClass("_3TzAhzBA-XQQruZs-bwWjE").text();
            //阅读人数
            String num = title.getElementsByClass("_2gvAnxa4Xc7IT14d5w8MI1").text();
            Entity entity = new Entity(name, num, date, url);
            entities.add(entity);
        }
        Thread.sleep(1000);
        System.out.println("网址为:"+u+"---->爬取完成");
    }

    /**
     * 获取总页数
     *
     * @return
     * @throws IOException
     */
    public static int getPage() throws IOException {
        Document document = Jsoup.connect("https://www.aquanliang.com/blog/page").get();
        String sum = document.getElementsByClass("ant-pagination-item ant-pagination-item-59").text();
        return Integer.parseInt(sum);
    }

    /**
     * 获取所有的数据
     * @throws IOException
     */
    public static void getALl() throws IOException {
        int page = getPage() + 1;
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 1; i < page-50; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 10; i < page-40; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 20; i < page-30; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 30; i < page-20; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 40; i < page-10; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                for (int i = 50; i < page; i++) {
                    getMsg(url + i);
                }
            }
        }).start();
    }
}
